package com.zhiyou100.api.java8_date;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

/**
 * java8日期格式化
 */
public class Demo4 {

    public static void main(String[] args) {
        testStringToDate();
    }

    //字符串转对应的LocalDate、LocalTime、LocalDateTime
    public static void testStringToDate(){
        String date = "2022-01-18 14:16:44";
        String date2 = "2022-01-18 星期二";
        LocalDateTime time = LocalDateTime.parse(date,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println("time = " + time);
        LocalDate localDate = LocalDate.parse(date2, DateTimeFormatter.ofPattern("yyyy-MM-dd E"));
        System.out.println("localDate = " + localDate);
    }

    //LocalDateTime转字符串
    public static void testDateToString(){
        //获取一个本地日期时间
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println("ldt = " + ldt);
        //调用format进行格式化    ISO_LOCAL_DATE   yyyy-MM-dd
        String s = ldt.format(DateTimeFormatter.ISO_LOCAL_DATE);
        System.out.println("s = " + s);
        //yyyyMMdd
        String s1 = ldt.format(DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println("s1 = " + s1);
        //yyyy-MM-ddTHH:mm:ss.n
        String s2 = ldt.format(DateTimeFormatter.ISO_DATE_TIME);
        System.out.println("s2 = " + s2);
        //手动指定格式
        String s3 = ldt.format(DateTimeFormatter.ofPattern("YYYY年MM月dd日 E"));
        System.out.println("s3 = " + s3);
        //手动指定格式的同时提供一个语言的国家
        String s4 = ldt.format(DateTimeFormatter.ofPattern("YYYY年MM月dd日 E", Locale.ENGLISH));
        System.out.println("s4 = " + s4);
    }

}
